// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   ResourceAction.java

package com.iphaeton.lms.action.resource;

import com.iphaeton.common.helper.*;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.lms.business.organize.IUserService;
import com.iphaeton.lms.business.resource.IResourceService;
import com.iphaeton.lms.pojo.resource.Resource;
import com.iphaeton.lms.pojo.resource.Sort;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
import org.dom4j.io.XMLWriter;

// Referenced classes of package com.iphaeton.lms.action.resource:
//            ResourceBizSupportAction

public class ResourceAction extends ResourceBizSupportAction
{

    public ResourceAction()
    {
    }

    public ActionForward resourceForm(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        String type = request.getParameter("type");
        if(id == null)
            id = "";
        List list;
        try
        {
            list = resource.getSort();
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("type", type);
        request.setAttribute("id", id);
        request.setAttribute("list", list);
        return mapping.findForward("resource_add");
    }

    public ActionForward addResource(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String type = request.getParameter("type");
        Calendar cal = Calendar.getInstance();
        java.util.Date time = cal.getTime();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String currentTime = df.format(time);
        qaf = (DynaActionForm)actionForm;
        int currentPage = 0;
        int pageSize = 10;
        String name = (String)qaf.get("title");
        String content = (String)qaf.get("intro");
        String sort = (String)qaf.get("sort");
        FormFile file = (FormFile)qaf.get("filename");
        String filename = file.getFileName();
        int size = file.getFileSize();
        String strSize = "";
        if(size > 0xf4240)
            strSize = strSize + (double)Math.round(size / 10000) / 100D + "M";
        else
            strSize = strSize + (double)Math.round(size / 10) / 100D + "K";
        Resource r = new Resource();
        String strPage = request.getParameter("page");
        List list = null;
        String map = "";
        try
        {
            UserSession userSession = (UserSession)request.getSession().getAttribute("userSession");
            String id = userSession.getId();
            r.setName(name);
            r.setComment(content);
            r.setCreate_time(currentTime);
            r.setDownload_count(new Integer(0));
            r.setFile_name(filename);
            r.setSize(strSize);
            r.setSort(sort);
            r.setS(resource.findSortById(sort));
            r.setState(new Integer(1));
            r.setUser_id(id);
            r.setUser(user.findUserById(id));
            if(type.equals("admin"))
            {
                map = "resource_list";
                r.setValid(new Integer(1));
            } else
            if(type.equals("user"))
            {
                map = "upload_ok";
                r.setValid(new Integer(2));
            }
            r = resource.persisteRes(r);
            upload.upload(file, request.getRealPath("/") + "upload" + File.separator + "resource" + File.separator + r.getId() + File.separator);
            if(type.equals("admin"))
            {
                if(strPage == null || strPage.trim().equals(""))
                    currentPage = 1;
                else
                if(strPage != null && !strPage.trim().equals(""))
                    currentPage = Integer.parseInt(strPage);
                pageInfo = resource.getSortResource(sort, 0, pageSize);
                pageInfo.setPage(currentPage);
                request.getSession().setAttribute("pageInfo", pageInfo);
                list = getResource().getSpecifiedResource(pageInfo);
            }
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("list", list);
        request.setAttribute("id", sort);
        return mapping.findForward(map);
    }

    public ActionForward selectSortList(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        List list = null;
        try
        {
            list = resource.getSort();
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("list", list);
        return mapping.findForward("sort_list");
    }

    public ActionForward addResourceSort(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        qaf = (DynaActionForm)actionForm;
        String name = (String)qaf.get("sort_name");
        String catalog = (String)qaf.get("sort_catalog");
        try
        {
            resource.addCatalog(name, catalog);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        return mapping.findForward("sortList");
    }

    public ActionForward editSort(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        Sort s = null;
        try
        {
            s = resource.findSortById(id);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("sort", s);
        return mapping.findForward("sort_edit");
    }

    public ActionForward removeResourceSort(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        try
        {
            resource.removeSort(id);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        return mapping.findForward("sortList");
    }

    public ActionForward updateSort(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        qaf = (DynaActionForm)actionForm;
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String catalog = request.getParameter("catalog");
        Sort s = null;
        try
        {
            s = resource.findSortById(id);
            s.setName(name);
            s.setCatalog(catalog);
            resource.updateSort(s);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        return mapping.findForward("sortList");
    }

    public ActionForward resourceTree(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String type = request.getParameter("type");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0L);
        org.dom4j.Document doc = null;
        try
        {
            doc = resource.getSortTree(type);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        response.setContentType("text/xml; charset=UTF-8");
        javax.servlet.ServletOutputStream sos = response.getOutputStream();
        XMLWriter writer = new XMLWriter(sos);
        writer.write(doc);
        writer.flush();
        writer.close();
        return null;
    }

    public ActionForward removeResource(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        int currentPage = 1;
        String strPage = request.getParameter("page");
        String sortId = request.getParameter("sortId");
        if(sortId == null)
            sortId = "";
        String id[] = request.getParameter("id").split(";");
        List list = null;
        try
        {
            for(int i = 0; i < id.length; i++)
                resource.removeResource(id[i]);

            if(strPage == null || strPage.trim().equals(""))
                currentPage = 1;
            else
            if(strPage != null && !strPage.trim().equals(""))
                currentPage = Integer.parseInt(strPage);
            pageInfo = resource.getSortResource(sortId, 0, 10);
            pageInfo.setPage(currentPage);
            request.getSession().setAttribute("pageInfo", pageInfo);
            list = getResource().getSpecifiedResource(pageInfo);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("list", list);
        request.setAttribute("id", sortId);
        return mapping.findForward("resource_list");
    }

    public ActionForward confirmRes(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        int currentPage = 1;
        String strPage = request.getParameter("page");
        String sortId = request.getParameter("sortId");
        String id[] = request.getParameter("id").split(";");
        if(sortId == null)
            sortId = "";
        Resource r = null;
        List list = null;
        try
        {
            for(int i = 0; i < id.length; i++)
            {
                r = resource.findResourceById(id[i]);
                r.setValid(new Integer(1));
                resource.persisteRes(r);
            }

            if(strPage == null || strPage.trim().equals(""))
                currentPage = 1;
            else
            if(strPage != null && !strPage.trim().equals(""))
                currentPage = Integer.parseInt(strPage);
            pageInfo = resource.getSortResource(sortId, 0, 10);
            pageInfo.setPage(currentPage);
            request.getSession().setAttribute("pageInfo", pageInfo);
            list = getResource().getSpecifiedResource(pageInfo);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("list", list);
        request.setAttribute("id", sortId);
        return mapping.findForward("resource_list");
    }

    public ActionForward getSortResource(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        if(id == null)
            id = "";
        int currentPage = 0;
        int pageSize = 10;
        String strPage = request.getParameter("page");
        List list;
        try
        {
            if(strPage == null || strPage.trim().equals(""))
                currentPage = 1;
            else
            if(strPage != null && !strPage.trim().equals(""))
                currentPage = Integer.parseInt(strPage);
            pageInfo = resource.getSortResource(id, 0, pageSize);
            pageInfo.setPage(currentPage);
            request.getSession().setAttribute("pageInfo", pageInfo);
            list = getResource().getSpecifiedResource(pageInfo);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("list", list);
        request.setAttribute("id", id);
        return mapping.findForward("resource_list");
    }

    public ActionForward resourceMoveTo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id[] = request.getParameter("id").split(";");
        List list = new LinkedList();
        List sort = null;
        try
        {
            for(int i = 0; i < id.length; i++)
                list.add(resource.findResourceById(id[i]));

            sort = resource.getSort();
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("list", list);
        request.setAttribute("sort", sort);
        request.setAttribute("id", request.getParameter("id"));
        return mapping.findForward("resource_move");
    }

    public ActionForward resourceMove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        int currentPage = 0;
        int pageSize = 10;
        String strPage = request.getParameter("page");
        List list = null;
        String id[] = request.getParameter("id").split(";");
        String sortId = request.getParameter("sort");
        try
        {
            for(int i = 0; i < id.length; i++)
            {
                Resource r = resource.findResourceById(id[i]);
                r.setSort(sortId);
                resource.persisteRes(r);
                if(strPage == null || strPage.trim().equals(""))
                    currentPage = 1;
                else
                if(strPage != null && !strPage.trim().equals(""))
                    currentPage = Integer.parseInt(strPage);
                pageInfo = resource.getSortResource(sortId, 0, pageSize);
                pageInfo.setPage(currentPage);
                request.getSession().setAttribute("pageInfo", pageInfo);
                list = getResource().getSpecifiedResource(pageInfo);
            }

        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("list", list);
        request.setAttribute("id", sortId);
        return mapping.findForward("resource_list");
    }

    public ActionForward userSortResource(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        if(id == null)
            id = "";
        int currentPage = 0;
        int pageSize = 10;
        String strPage = request.getParameter("page");
        List list;
        try
        {
            if(strPage == null || strPage.trim().equals(""))
                currentPage = 1;
            else
            if(strPage != null && !strPage.trim().equals(""))
                currentPage = Integer.parseInt(strPage);
            pageInfo = resource.getSortResource(id, 1, pageSize);
            pageInfo.setPage(currentPage);
            request.getSession().setAttribute("pageInfo", pageInfo);
            list = getResource().getSpecifiedResource(pageInfo);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("list", list);
        request.setAttribute("id", id);
        return mapping.findForward("user_resource");
    }

    public ActionForward download(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        Resource res;
        try
        {
            res = resource.findResourceById(id);
            res.setDownload_count(new Integer(res.getDownload_count().intValue() + 1));
            res = resource.persisteRes(res);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        String name = res.getFile_name();
        String url = request.getRealPath("/") + "upload" + File.separator + "resource" + File.separator + res.getId() + File.separator + name;
        request.setAttribute("url", url);
        request.setAttribute("name", name);
        return mapping.findForward("download");
    }

    private DynaActionForm qaf;
    private Pagination pageInfo;
}
